<?php

namespace app\beautiful\controller\admin\project;

 use app\BaseController;

 use app\common\logic\DataLogic;

 use app\beautiful\model\project\ProjectModel;

 use app\beautiful\model\project\ProjectTypeModel;

 use app\beautiful\model\project\SnapUpModel;



 class Snapup extends BaseController{

     protected $name='抢购';



     public function index()

     {

         $abbreviation=$this->request->param('abbreviation');

         $project_type_id=$this->request->param('project_type_id/d');

         $bg_take_count=$this->request->param('bg_take_count/d');

         $end_take_count=$this->request->param('end_take_count/d');

         $status=$this->request->param('status/d',1);

         $is_flash_sale=$this->request->param('is_flash_sale/d',0);

         if(!in_array($status,[1,-1])) $status=1;



         $where=[

             ['shop_id','=',$this->request->shopId],

             ['is_delete','=',0],

             ['status','=',$status]

         ];



         if(!empty($bg_take_count)){

             if(!empty($end_take_count)){

                 $where[]=['take_count','between',[$bg_take_count,$end_take_count]];

             }else{

                 $where[]=['take_count','>=',$bg_take_count];

             }

         }else{

             if(!empty($end_take_count)){

                 $where[]=['take_count','<=',$end_take_count];

             }

         }



         $today=date("Y-m-d",$this->request->time());





         if(!empty($project_type_id)) $where[]=['project_type_id','=',$project_type_id];



         $project_where=[

             ['shop_id','=',$this->request->shopId],

             ['is_delete','=',0],

         ];

         if(!empty($abbreviation)) $project_where[]=['abbreviation','=',$abbreviation];

         $project_ids= ProjectModel::where($project_where)->column('project_id') ?? [];

         $where[]=['project_id','in',$project_ids];

         $snap_up_model = new SnapUpModel();

         if($is_flash_sale==1){

             $where[]=['is_flash_sale','=',1];

             $where[]=['start_time','<=',$today];

             $where[]=['end_time','>=',$today];

             $where[]=['flash_num','>',0];

         }else{

             $snap_up_model=$snap_up_model->whereRaw("(start_time > '{$today}' or end_time < '{$today}'  or flash_num <= 0) OR is_flash_sale = 0");

         }

         $snap_up_model=$snap_up_model->with(['type','project']);

         $order=['sort'=>'desc','add_time'=>'desc'];

         $field='*';

         $append=['add_time_format','is_flash_sale_expire'];

         $return = DataLogic::getDataList($snap_up_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);

         if($this->request->page==1){

             $type_list=ProjectTypeModel::where([

                 ['shop_id','=',$this->request->shopId],

                 ['is_delete','=',0]

             ])->field('project_type_id,name')->select()->toArray();

             $return['type_list']=$type_list;

         }

         return $this->success($return);



     }



     public function show()

     {

         $snap_up_id=$this->request->param('snap_up_id/d',0);

         if($snap_up_id==0) return $this->success([]);



         if(null === $snap_up = SnapUpModel::find($snap_up_id)) return $this->error($this->name.'不存在');

         if($snap_up->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');

         if($snap_up->is_delete != 0) return $this->error($this->name.'不存在');



         if(null === $project = ProjectModel::find($snap_up->project_id))  return $this->error($this->name.'不存在');

         if($project->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');

         if($project->is_delete != 0) return $this->error($this->name.'不存在');





         $detail=[

            'project'=>[

                'project_id'=>$project->project_id,

                'abbreviation'=>$project->abbreviation,

                'market_price'=>$project->market_price,

                'gross'=>$project->gross,

                'cover_img'=>$project->cover_img,

            ],

             'vip_price'=>$snap_up->vip_price,

             'gross'=>$snap_up->gross,

             'is_flash_sale'=>$snap_up->is_flash_sale,

             'flash_price'=>$snap_up->flash_price,

             'flash_num'=>$snap_up->flash_num,

             'flash_sale_time'=>$snap_up->flash_sale_time,

             'valid_time'=>$snap_up->valid_time,

             'valid_unit'=>$snap_up->valid_unit,

             'fictitious_take_count'=>$snap_up->fictitious_take_count,

             'rotation'=>$snap_up->rotation,

             'snap_up_details'=>$snap_up->snap_up_details,

             'purchase_information'=>$snap_up->purchase_information,

             'sort'=>$snap_up->sort,

         ];



         return $this->success(['detail'=>$detail]);

     }



     public function chooseProject()

     {

         $name= $this->request->param('name');

        $where=[

             ['shop_id','=',$this->request->shopId],

             ['is_delete','=',0],

        ];

        if(!empty($name)) $where[]=['abbreviation','=',$name];



         //判断项目的唯一性

         $project_ids=SnapUpModel::where([

             ['shop_id','=',$this->request->shopId],

             ['is_delete','=',0],

         ])->column('project_id') ?? [];

         $where[]=['project_id','not in',$project_ids];



         $project_model= new ProjectModel();

         $order=['add_time'=>'desc'];

         $field='*';

         $append=['add_time_format'];

         $return = DataLogic::getDataList($project_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);

         return $this->success($return);

     }



     public function save()

     {

         $snap_up_id=$this->request->param('snap_up_id/d',0);

         $project_id=$this->request->param('project_id/d',0);

         $vip_price=$this->request->param('vip_price/f',0);

         $is_flash_sale=$this->request->param('is_flash_sale/d',0);

         $flash_price=$this->request->param('flash_price/f',0);

         $gross=$this->request->param('gross/f',0);

         $flash_num=$this->request->param('flash_num/d',0);

         $flash_sale_time=$this->request->param('flash_sale_time/a',[]);

         $valid_time=$this->request->param('valid_time/d',0);

         $valid_unit=$this->request->param('valid_unit');

         $fictitious_take_count=$this->request->param('fictitious_take_count/d',0);

         $rotation=$this->request->param('rotation/a',[]);

         $snap_up_details=$this->request->param('snap_up_details','','security_json_or_array');

         $purchase_information=$this->request->param('purchase_information');

         $sort=$this->request->param('sort/d',0);



         //判断传来的参数

         if(null === $project = ProjectModel::find($project_id))  return $this->error('项目不存在');

         if($project->shop_id != $this->request->shopId) return $this->error('项目不存在');

         if($project->is_delete != 0) return $this->error('项目不存在');

         //判断项目的唯一性 （后期商榷在添加）



         if(empty($vip_price) || $vip_price<=0) return $this->error('请输入会员价，且不能小于0');

         if($vip_price> $project->market_price) return $this->error('会员价不能高于项目市场价');

         if(empty($gross) || $gross<=0) return $this->error('请输入毛利价，且不能小于0');

         if($gross> $vip_price) return $this->error('毛利价不能高于项目会员价');

         if(!in_array($is_flash_sale,[0,1])) return $this->error('参数错误');

         if($is_flash_sale==1){

             if(empty($flash_price) || $flash_price<=0) return $this->error('请输入秒杀价,且不能小于0');

             if($flash_price>$vip_price) return $this->error('秒杀价不能大于会员价');

             if(empty($flash_num) || $flash_num<=0) return $this->error('秒杀数量不能小于0');

             if(empty($flash_sale_time) ||count($flash_sale_time) != 2) return $this->error('请选择秒杀时间');

             $start_time=$flash_sale_time[0]; $end_time=$flash_sale_time[1];

             if(false === $start_time_unix = strtotime($start_time)) return $this->error('请选择正确的秒杀开始时间');

             if(false === $end_time_unix = strtotime($end_time)) return $this->error('请选择正确的秒杀结束时间');

             if($end_time_unix<$start_time_unix) return $this->error('秒杀开始时间不能大于结束时间');

         }

         if(empty($valid_time) || $valid_time<=0) return $this->error('请输入有效期的值,且不能小于0');

         if(empty($valid_unit) || !in_array($valid_unit,['day','month','year'])) return $this->error('请选择有效期单位');

         if(empty($fictitious_take_count) || $fictitious_take_count <=0) return $this->error('请输入需要展示的销量');

         if(empty($rotation) || !is_array($rotation) || count($rotation)>5) return $this->error('请上传轮播图，且数量不能多余5个');

         if(empty($snap_up_details)) return $this->error('请填写秒杀详情');

         if(empty($purchase_information)) return $this->error('请输入购买须知');

         if($sort<0) return $this->error('请输入排序，且不能小于0');





         //操作

         if($snap_up_id != 0){

             if(null === $snap_up = SnapUpModel::find($snap_up_id)) return $this->error($this->name.'不存在');

             if($snap_up->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');

             if($snap_up->is_delete != 0) return $this->error($this->name.'不存在');

             $pre_rotation=$snap_up->rotation;$pre_snap_up_details=$snap_up->snap_up_details;

         }else{

             $snap_up= new SnapUpModel();

             $snap_up->shop_id = $this->request->shopId;

             $pre_rotation=[];$pre_snap_up_details=[];

         }



         $snap_up->project_id=$project_id;

         $snap_up->project_type_id=$project->project_type_id;

         $snap_up->vip_price=$vip_price;

         $snap_up->gross=$gross;

         $snap_up->is_flash_sale=$is_flash_sale;

         if($is_flash_sale==1){

             $snap_up->flash_price=$flash_price;

             $snap_up->flash_num=$flash_num;

             $snap_up->start_time=$start_time;

             $snap_up->end_time=$end_time;

         }

         $snap_up->valid_time=$valid_time;

         $snap_up->valid_unit=$valid_unit;

         $snap_up->fictitious_take_count=$fictitious_take_count;

         $snap_up->rotation=$rotation;

         $snap_up->snap_up_details=$snap_up_details;

         $snap_up->purchase_information=$purchase_information;

         $snap_up->sort=$sort;

         $snap_up->status=1;

         $snap_up->save();



         //图片操作

         arrayNoUseImg($pre_rotation); arrayUseImg($rotation);

         arrayNoUseImg($pre_snap_up_details); arrayUseImg($snap_up_details);



         return $this->success('保存成功');

     }



     public function undercarriage()

     {

         $snap_up_id=$this->request->param('snap_up_id/d',0);

         if(null === $snap_up = SnapUpModel::find($snap_up_id)) return $this->error($this->name.'不存在');

         if($snap_up->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');

         if($snap_up->is_delete != 0) return $this->error($this->name.'不存在');

         if($snap_up->status != 1) return $this->error($this->name.'已下架，请勿重复操作');



         $snap_up->status=-1;

         $snap_up->save();

         return $this->success('下架成功');

     }



     public function grounding()

     {

         $snap_up_id=$this->request->param('snap_up_id/d',0);

         if(null === $snap_up = SnapUpModel::find($snap_up_id)) return $this->error($this->name.'不存在');

         if($snap_up->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');

         if($snap_up->is_delete != 0) return $this->error($this->name.'不存在');

         if($snap_up->status != -1) return $this->error($this->name.'已上架，请勿重复操作');



         $snap_up->status=1;

         $snap_up->save();

         return $this->success('上架成功');

     }



     public function del()

     {

         $snap_up_id=$this->request->param('snap_up_id/d',0);

         if(null === $snap_up = SnapUpModel::find($snap_up_id)) return $this->error($this->name.'不存在');

         if($snap_up->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');

         if($snap_up->is_delete != 0) return $this->error($this->name.'不存在');

         if($snap_up->status != -1) return $this->error($this->name.'还未下架，请先下架在删除');



         $snap_up->is_delete=1;

         $snap_up->save();

         return $this->success('删除成功');

     }

 }

